package cn.edu.ecut.multiton;

/**
 * "懒汉式"多例 ( Multiton ) 
 */
public class Gender {
	
	private static Gender female ;  // 女
	private static Gender male ; // 男
	
	private int index ;
	private String name ;
	
	private Gender(int index, String name) {
		super();
		this.index = index;
		this.name = name;
	}

	@Override
	public String toString() {
		return "Gender [index=" + index + ", name=" + name + "]";
	}
	
	public static Gender of( int index ) {
		switch ( index ) {
			case 1 : return of( "女" );
			case 2 : return of( "男" );
			default:
				return null ;
		}
	}
	
	public static Gender of( String name ) {
		switch ( name ) {
			case "女":
				if( female == null ) {
					female = new Gender( 1 , "女" );
				}
				return female ;
			case "男":
				if( male == null ) {
					male = new Gender( 2 , "男" );
				}
				return male ;
			default:
				return null ;
		}
	}

}
